
//
// 110405 - AH - (Bybanen *) Created.
//

package org.soekrat.engine.pitbul.parser;

/**
 * <p>
 *   Tokenizer used for Pitbul.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class TokenizerOld 
{
  private final String m_txt;
  
  private int m_pos=0;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param txt Text to scan for tokens.
   */
  public TokenizerOld(String txt)
  {
    if (txt==null)
    {
      throw new NullPointerException("txt");
    }
    
    m_txt=txt;
  }
  
  private static enum State { START, W, C, Q, END };
  
  private final StringBuilder m_sb=new StringBuilder();
  
  private boolean m_quoted=false;
  
  /**
   * <p>
   *   Tells if last token from 
   *   {@link #next()} was quoted or not.
   * </p>
   * @return {@code true} if was.
   */
  public boolean isQuoted(){ return m_quoted; }
  
  /**
   * <p>
   *   Gets next token.
   * </p>
   * @return Token or {@code null} if no more tokens.
   */
  public String next()
  {
    State state=State.START;
    
    boolean quote=false;
    
    char c=' ';
    
    while (true)
    {
      switch (state)
      {
        case START :
          
          if (m_pos==m_txt.length()) 
          {
            state=State.END;
          }
          else
          {
            c=m_txt.charAt(m_pos++);
          
                 if (Character.isWhitespace(c)) state=State.W;
            else if (c=='\'')                   state=State.Q;
            else                                state=State.C;
          }
          
        break;
        
        case W :
          
          if (quote)
          {
            m_sb.append(c);
          }
          else
          {
            if (m_sb.length()>0) 
            {
              String retVal=m_sb.toString();
              m_sb.setLength(0);
              m_quoted=false;
              return retVal;
            }
          }
          
         if (m_pos==m_txt.length()) state=State.END;
         else                       state=State.START;
          
        break;
      
        case C :
          
          m_sb.append(c);
          
          state=State.START;
          
        break;
      
        case Q :
          
          if (quote)
          {
            quote=false;
            String retVal=m_sb.toString();
            m_sb.setLength(0);
            m_quoted=true;
            return retVal;
          }
          else
          {
            quote=true;
            if (m_sb.length()>0)
            {
              String retVal=m_sb.toString();
              m_sb.setLength(0);
              m_quoted=false;
              m_pos--;
              return retVal;
            }
          }
          
          state=State.START;
          
        break;
      
        case END :
          
          String retVal=m_sb.toString();
          m_sb.setLength(0);
          
          if (retVal.length()==0) 
          {
            m_quoted=false;
            return null;
          }
          else
          {
            if (quote) m_quoted=true;
            return retVal;
          }
      
        default: throw new Error();
      }
    }
  }
  
  public static void main(String[] args) 
  {
    String txt="mime-type 'image/jpg' in ontology mine ;";
    
    //String txt="a'he he'b";
    
    TokenizerOld t=new TokenizerOld(txt);
    String token=t.next();
    while (token!=null)
    {
      System.out.println("'"+token+"' was quoted: "+t.isQuoted());
      token=t.next();
    }
  }
  
}
